import CryptoJS from 'crypto-js'

const secretKey = 'mhxzkhl@2024gzx.'

export function aesEncrypt(text) {
	const ciphertext = CryptoJS.AES.encrypt(text, CryptoJS.enc.Utf8.parse(secretKey), {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	})
	return ciphertext.toString()
}

export function aesDecrypt(ciphertext) {
	try {
		const bytes = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(secretKey), {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		})
		return bytes.toString(CryptoJS.enc.Utf8).length < 10 ? ciphertext : bytes.toString(CryptoJS.enc.Utf8)
	} catch (e) {
		return ciphertext;
	}
}
